package rottenbren.dev.android.diary.dao;

import java.util.HashMap;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import rottenbren.dev.android.diary.model.Aniversary;
import rottenbren.dev.android.diary.util.DBOpenHelper;

public class CheckAniversary {
	
	private static HashMap<String, Aniversary> SOLAR_ANIVERSARY = null;
	private static HashMap<String, Aniversary> LUNAR_ANIVERSARY = null;
	
	private Context context;

	public CheckAniversary(Context context){
		this.context = context;
		initAniversaryMap();
	}

	private void initAniversaryMap(){
		SOLAR_ANIVERSARY = new HashMap<String, Aniversary>();
		LUNAR_ANIVERSARY = new HashMap<String, Aniversary>();
		DBOpenHelper dbOpenHelper = new DBOpenHelper(context, "rottenbren.diary.db", null, 1);
		SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
		
		Cursor c = db.rawQuery("SELECT type, month, day, title, isHoliday FROM aniversary_mst", null);
		c.moveToFirst();
		while(c.isAfterLast() == false){
			if(c.getString(1).equals("lunar")){
				Aniversary aniversary = new Aniversary();
				aniversary.setTitle(c.getString(4));
				aniversary.setHoliday(Boolean.parseBoolean(c.getString(5)));
				LUNAR_ANIVERSARY.put((c.getInt(2) < 10 ? "0" + c.getString(2) : c.getString(2)) + (c.getInt(3) < 10 ? "0" + c.getString(3) : c.getString(3)), aniversary);
			}else{
				Aniversary aniversary = new Aniversary();
				aniversary.setTitle(c.getString(4));
				aniversary.setHoliday(Boolean.parseBoolean(c.getString(5)));
				SOLAR_ANIVERSARY.put((c.getInt(2) < 10 ? "0" + c.getString(2) : c.getString(2)) + (c.getInt(3) < 10 ? "0" + c.getString(3) : c.getString(3)), aniversary);
			}
		}
	}
	
	public Aniversary getSolarAniversary(String key){
		return SOLAR_ANIVERSARY.get(key);
	}
	
	public Aniversary getLunarAniversary(String key){
		return LUNAR_ANIVERSARY.get(key);
	}
	
}
